package com.yonyou.rabbitmq.receiver.basic.worker;

import com.rabbitmq.client.*;
import com.yonyou.rabbitmq.utils.RabbitmqUtils;

import java.io.IOException;

public class Consumer2 {

    public static void main(String[] args) {
        Connection connection = null;
        Channel channel = null;

        try {
            //获取连接
            connection = RabbitmqUtils.getConnection();
            if (connection != null) {
                //获取通道
                channel = connection.createChannel();
                //声明队列
                channel.queueDeclare("work",true,false,false,null);

                //消费消息
                channel.basicConsume("work",true,new DefaultConsumer(channel){
                    @Override
                    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                        System.out.println("Consumer-2:"+new String(body));//打印消息
                    }
                });
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
